package com.mes.cloud.service;

import com.mes.cloud.api.dto.request.product.ProductUnitAddRequest;
import com.mes.cloud.api.dto.request.product.ProductUnitRequest;
import com.mes.cloud.api.dto.request.product.ProductUnitUpdateRequest;
import com.mes.cloud.api.dto.response.ProductUnitDto;
import com.mes.cloud.exception.BaseException;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.constant.Constants;
import com.mes.cloud.dao.ProductUnitDao;
import com.mes.cloud.entity.ProductLog;
import com.mes.cloud.entity.ProductUnit;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.util.DateUtil;
import com.mes.cloud.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 产品单位
 *
 * @author xy
 * @version 2019-08-23 17:43:03
 * @email yuan.xue3@pactera.com
 */
@Service
public class ProductUnitService {

    private ProductUnitDao productUnitDao;

    private IPKeyGenerator ipKeyGenerator;

    private LogService logService;


    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    public ProductUnitService(ProductUnitDao productUnitDao
            , IPKeyGenerator ipKeyGenerator
            , LogService logService) {
        this.productUnitDao = productUnitDao;
        this.ipKeyGenerator = ipKeyGenerator;
        this.logService = logService;
    }

    /**
     * 查列表
     *
     * @param productUnitRequest
     * @return JsonResult
     */
    public JsonResult<List<ProductUnitDto>> list(ProductUnitRequest productUnitRequest) {
        //查询条件
        ProductUnit selectParam = new ProductUnit();
        BeanUtils.copyProperties(productUnitRequest, selectParam);
        List<ProductUnit> productUnitList = productUnitDao.select(selectParam);
        List<ProductUnitDto> productUnitDtoList = new ArrayList<>();
        for (ProductUnit productUnit : productUnitList) {
            ProductUnitDto productUnitDto = new ProductUnitDto();
            BeanUtils.copyProperties(productUnit, productUnitDto);
            productUnitDtoList.add(productUnitDto);
        }
        JsonResult<List<ProductUnitDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(productUnitDtoList);
        return jsonResult;
    }

    /**
     * 新增
     *
     * @param productUnitRequest
     * @return JsonResult
     */
    public JsonResult add(ProductUnitAddRequest productUnitRequest) {
        String productId = productUnitRequest.getProductId();
        String conversionRatio = productUnitRequest.getConversionRatio();
        String type = "";
        JsonResult jsonResult = new JsonResult();
        //如果换算比为1是基本单位,一个产品只有一个基本单位,检查是否已经存在基本单位
        if (Constants.CONVERSION_RATIO.equals(conversionRatio)) {
            type = Constants.BASIC_UNIT;
            ProductUnit queryParams = new ProductUnit();
            queryParams.setProductId(productId);
            queryParams.setType(type);
            int count = productUnitDao.selectCount(queryParams);
            if (count > 0) {
                jsonResult.setMsg(HttpStatusEnum.PRODUCT_BASIC_UNIT_EXIST.getMessage()).setCode(HttpStatusEnum.PRODUCT_BASIC_UNIT_EXIST.getCode());
                return jsonResult;
            }
        } else {
            type = Constants.AUXILIARY_UNIT;
        }
        ProductUnit productUnit = new ProductUnit();
        BeanUtils.copyProperties(productUnitRequest, productUnit);
        productUnit.setId(ipKeyGenerator.getGenerateKey());
        productUnit.setType(type);
        productUnit.setUpdateDateStr(DateUtil.updateDateStr());
        productUnit.setCreateBy(UserUtil.getCreateByName());
        productUnit.setUpdateBy(UserUtil.getUpdateByName());
        productUnit.setCreateByNo(UserUtil.getCreateByNo());
        productUnit.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = productUnitDao.insertSelective(productUnit);
        if (count > 0) {
            insertLog(productId, Constants.ADD_TO_PRODUCT_UNIT);
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 更新
     *
     * @param productUnitUpdateRequest
     * @return JsonResult
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult update(ProductUnitUpdateRequest productUnitUpdateRequest) {
        String productId = productUnitUpdateRequest.getProductId();
        String conversionRatio = productUnitUpdateRequest.getConversionRatio();
        JsonResult jsonResult = new JsonResult();
        //如果换算比为1是基本单位,一个产品只有一个基本单位,检查是否已经存在基本单位
        if (Constants.CONVERSION_RATIO.equals(conversionRatio)) {
            productUnitUpdateRequest.setType(Constants.BASIC_UNIT);
            ProductUnit queryParams = new ProductUnit();
            queryParams.setProductId(productId);
            queryParams.setType(Constants.BASIC_UNIT);
            queryParams = productUnitDao.selectOne(queryParams);
            String id = productUnitUpdateRequest.getId();
            if (Objects.nonNull(queryParams) && !id.equals(queryParams.getId())) {
                jsonResult.setMsg(HttpStatusEnum.PRODUCT_BASIC_UNIT_EXIST.getMessage()).setCode(HttpStatusEnum.PRODUCT_BASIC_UNIT_EXIST.getCode());
                return jsonResult;
            }
        }

        ProductUnit productUnit = new ProductUnit();
        BeanUtils.copyProperties(productUnitUpdateRequest, productUnit);
        productUnit.setUpdateDateStr(DateUtil.updateDateStr());
        productUnit.setUpdateBy(UserUtil.getUpdateByName());
        productUnit.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = productUnitDao.updateByPrimaryKeySelective(productUnit);

        if (count > 0) {
            insertLog(productId, Constants.UPDATE_TO_PRODUCT_UNIT);
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 查看详情
     *
     * @param id
     * @return JsonResult
     */
    public JsonResult<ProductUnitDto> get(String id) {
        JsonResult jsonResult = new JsonResult();
        ProductUnit productUnit = productUnitDao.selectByPrimaryKey(id);
        if (productUnit == null) {
            return jsonResult.setCode(HttpStatusEnum.RECORD_NOT_EXIST.getCode()).setMsg(HttpStatusEnum.RECORD_NOT_EXIST.getMessage());
        }
        ProductUnitDto productUnitDto = new ProductUnitDto();
        BeanUtils.copyProperties(productUnit, productUnitDto);
        return jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(productUnitDto);
    }

    private void insertLog(String id, String logMessage) {
        ProductLog productLog = new ProductLog();
        productLog.setProductId(id);
        productLog.setMenuCode(Constants.PRODUCT_MENU_CODE);
        productLog.setLog(logMessage);
        productLog.setId(ipKeyGenerator.getGenerateKey());
        productLog.setUpdateDateStr(DateUtil.updateDateStr());
        productLog.setCreateBy(UserUtil.getCreateByName());
        productLog.setUpdateBy(UserUtil.getUpdateByName());
        productLog.setCreateByNo(UserUtil.getCreateByNo());
        productLog.setUpdateByNo(UserUtil.getUpdateByNo());
        logService.insertProductLog(productLog);
    }
}
